﻿using System;
using System.Collections.Generic;
using System.IO;
using FeedsLibrary;
using FeedsLibrary.Feeds.Abstract;
using FeedsLibrary.Feeds.Atom;
using FeedsLibrary.Feeds.RSS;
using FeedsLibrary.Feeds.RDF;
using FeedsLibrary.Feeds;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace RSSUnitTests
{
    [TestClass]
    [DeploymentItem("atom.xml", "")]
    [DeploymentItem("rss.xml", "")]
    [DeploymentItem("rdf.xml", "")]
    [DeploymentItem("Feeds.xml", "")]
    [DeploymentItem("favicon.ico", "")]

    public class RssUnitTests
    {
        [TestMethod]
        public void TestRdfDocument()
        {
            DateTime date = new DateTime(2015, 3, 11);
            Uri ru = new Uri(Path.Combine(Directory.GetCurrentDirectory(), "rdf.xml"));
            Uri iu = new Uri(Path.Combine(Directory.GetCurrentDirectory(), "favicon.ico"));

            IChannel c = ChannelFactory.CreateFeed(ru, iu, date, 1, false);
            c.Save("save_rdf.xml");

            RDFChannel a = c as RDFChannel;
            Assert.AreEqual("rdf title", a.Title);
            Assert.AreEqual(10, a.Items.Count);
            Assert.AreEqual("item1", a.Items[0].Title);
            Assert.IsTrue(File.Exists("save_rdf.xml"));
        }

        [TestMethod]
        public void TestRssDocument()
        {
            DateTime date = new DateTime(2014, 11, 11);
            Uri ru = new Uri(Path.Combine(Directory.GetCurrentDirectory(), "rss.xml"));
            Uri iu = new Uri(Path.Combine(Directory.GetCurrentDirectory(), "favicon.ico"));
            IChannel c = ChannelFactory.CreateFeed(ru, iu, date, 100, false);
            c.Save("save_rss.xml");

            RssChannel a = c as RssChannel;
            Assert.AreEqual("rss title", a.Title );
            Assert.AreEqual(10, a.Items.Count);
            Assert.AreEqual("item1", a.Items[0].Title);
            Assert.IsTrue(File.Exists("save_rss.xml"));
        }

        [TestMethod]
        public void TestAtomDocument()
        {
            DateTime date = new DateTime(2014, 11, 11);
            Uri ru = new Uri(Path.Combine(Directory.GetCurrentDirectory(), "atom.xml"));
            Uri iu = new Uri(Path.Combine(Directory.GetCurrentDirectory(), "favicon.ico"));

            IChannel c = ChannelFactory.CreateFeed(ru, iu, date, 100, false);
            c.Save("save_atom.xml");

            AtomChannel a = c as AtomChannel;

            Assert.AreEqual("atom title", a.Title);
            Assert.AreEqual(10, a.Items.Count);
            Assert.AreEqual("item1", a.Items[0].Title);
            Assert.IsTrue(File.Exists("save_atom.xml"));
        }

        [TestMethod]
        public void TestUserSettings()
        {
            UserSettings u = new UserSettings("");
            Assert.AreEqual(u.FeedCount(),3);

            FeedDetail f = u.FeedDetails(0);
            Assert.AreEqual(f.ChannelDetails.Count, 1);
            Assert.AreEqual(f.Name, "feed1");
            FeedDetail f1 = u.FeedDetails(1);
            Assert.AreEqual(f1.ChannelDetails.Count, 2);
            Assert.AreEqual(f1.Name, "feed2");
            FeedDetail f2 = u.FeedDetails(2);
            Assert.AreEqual(f2.ChannelDetails.Count, 3);
            Assert.AreEqual(f2.Name, "feed3");
        }

        [TestMethod]
        public void TestCompoundChannel()
        {
            DateTime date = new DateTime(2014, 11, 11);
            Uri rdu = new Uri(Path.Combine(Directory.GetCurrentDirectory(), "rss.xml"));
            Uri rsu = new Uri(Path.Combine(Directory.GetCurrentDirectory(), "rss.xml"));
            Uri aru = new Uri(Path.Combine(Directory.GetCurrentDirectory(), "atom.xml"));
            Uri iu = new Uri(Path.Combine(Directory.GetCurrentDirectory(), "favicon.ico"));

            ChannelDetail cd = new ChannelDetail();
            cd.Url = rsu;
            cd.IconUrl = iu;

            ChannelDetail cd2 = new ChannelDetail();
            cd2.Url = aru;
            cd2.IconUrl = iu;

            ChannelDetail cd3 = new ChannelDetail();
            cd3.Url = rdu;
            cd3.IconUrl = iu;

            FeedDetail f = new FeedDetail();
            f.Name = "fd1";
            f.Days = 100;
            f.TagsOnly = false;
            f.ChannelDetails = new List<ChannelDetail>{cd, cd2, cd3};
            CompoundChannel cc= new CompoundChannel(f, date, "");

            Assert.AreEqual("fd1", cc.Description);
            Assert.AreEqual(30, cc.ItemCount());
        }
    }
}
